﻿using System;
using System.Windows;
using System.Windows.Input;
using JelleDruyts.Windows;
using JelleDruyts.Windows.ObjectModel;
using Schedulr.Infrastructure;
using Schedulr.Messages;
using Schedulr.Models;

namespace Schedulr.ViewModels
{
    public class OptionsViewModel : ViewModel
    {
        #region Fields

        private Account account;

        #endregion

        #region Properties

        public ICommand AddFolderToMonitorCommand { get; private set; }
        public ICommand RemoveFolderToMonitorCommand { get; private set; }
        public ICommand DiscoverMonitoredFoldersCommand { get; private set; }

        #endregion

        #region Observable Properties

        /// <summary>
        /// Gets or sets the picture defaults.
        /// </summary>
        public PictureDetailsViewModel PictureDefaults
        {
            get { return this.GetValue(PictureDefaultsProperty); }
            set { this.SetValue(PictureDefaultsProperty, value); }
        }

        /// <summary>
        /// The definition for the <see cref="PictureDefaults"/> observable property.
        /// </summary>
        public static ObservableProperty<PictureDetailsViewModel> PictureDefaultsProperty = new ObservableProperty<PictureDetailsViewModel, OptionsViewModel>(o => o.PictureDefaults);

        /// <summary>
        /// Gets or sets the account settings.
        /// </summary>
        public AccountSettings AccountSettings
        {
            get { return this.GetValue(AccountSettingsProperty); }
            set { this.SetValue(AccountSettingsProperty, value); }
        }

        /// <summary>
        /// The definition for the <see cref="AccountSettings"/> observable property.
        /// </summary>
        public static ObservableProperty<AccountSettings> AccountSettingsProperty = new ObservableProperty<AccountSettings, OptionsViewModel>(o => o.AccountSettings);

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="OptionsViewModel"/> class.
        /// </summary>
        public OptionsViewModel()
        {
            Messenger.Register<AccountActionMessage>(OnAccountActionMessage);
            this.AddFolderToMonitorCommand = new RelayCommand(AddFolderToMonitor, CanAddFolderToMonitor, "Add Folder To Monitor", "Adds a folder to monitor for new files");
            this.RemoveFolderToMonitorCommand = new RelayCommand(RemoveFolderToMonitor, CanRemoveFolderToMonitor, "Remove Folder To Monitor", "Stops monitoring the folder for new files");
            this.DiscoverMonitoredFoldersCommand = new RelayCommand(DiscoverMonitoredFolders, CanDiscoverMonitoredFolders, "Discover Files In Monitored Folders", "Adds new files in monitored folders to the queue");
        }

        #endregion

        #region Message Handlers

        private void OnAccountActionMessage(AccountActionMessage message)
        {
            if (message.Action == ListAction.CurrentChanged)
            {
                this.account = message.Account;
                if (this.account != null)
                {
                    this.AccountSettings = this.account.Settings;
                    this.PictureDefaults = new PictureDetailsViewModel(this.account, new Picture[] { this.account.Settings.PictureDefaults }, this.account.Settings.PictureDefaultsDetailsUISettings, Visibility.Collapsed, Visibility.Collapsed, false);
                }
                else
                {
                    this.AccountSettings = null;
                    this.PictureDefaults = null;
                }
            }
        }

        #endregion

        #region Commands

        private bool CanAddFolderToMonitor(object parameter)
        {
            return (this.AccountSettings != null);
        }

        private void AddFolderToMonitor(object parameter)
        {
            var dialog = new System.Windows.Forms.FolderBrowserDialog();
            dialog.Description = "Please select a folder to monitor for new files. All new pictures and videos in the selected folder (or any of its subfolders) will automatically be added to the queue.";
            dialog.SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
            var result = dialog.ShowDialog();
            if (result == System.Windows.Forms.DialogResult.OK)
            {
                this.AccountSettings.FoldersToMonitor.Add(dialog.SelectedPath);
            }
        }

        private bool CanRemoveFolderToMonitor(object parameter)
        {
            return (this.AccountSettings != null && this.AccountSettings.FoldersToMonitor.Contains(parameter as string));
        }

        private void RemoveFolderToMonitor(object parameter)
        {
            this.AccountSettings.FoldersToMonitor.Remove(parameter as string);
        }

        private bool CanDiscoverMonitoredFolders(object parameter)
        {
            return (this.AccountSettings != null && this.AccountSettings.FoldersToMonitor.Count > 0);
        }

        private void DiscoverMonitoredFolders(object parameter)
        {
            Tasks.AddFilesFromMonitoredFolders(this.account);
        }

        #endregion
    }
}